<template>
	<view class="user">
		<!-- 上方显示用户信息的区域 -->
		<view class="userinfo">
			<!-- 信息区域 -->
			<view class="top">
				<view class="avator">
					<uni-icons type="person-filled" size="50" :color="store.isLogin ? '#007AFF' : '#ccc'"></uni-icons>
				</view>
				<view class="uname" v-if="store.isLogin">
					<view class="username">{{ store.userinfo.username }}</view>
					<view class="mobile">{{ store.userinfo.mobilePhoneNumber }}</view>
				</view>
				<view class="uname" v-else @tap="toLogin">点击登录</view>
			</view>
		</view>
		<!-- 下方显示的四项用户操作项 -->
		<view class="list">
			<uni-list>
				<uni-list-item v-for="(item, index) in manageList" :key="index" :title="item.name" :show-extra-icon="true"
					:extra-icon="item.icon" clickable link @tap="navto(index)"></uni-list-item>
			</uni-list>
		</view>
	</view>
	<uni-popup ref="popup" type="dialog">
		<uni-popup-dialog title="通知" content="你确定要退出登录吗？" :duration="2000" @confirm="confirm"></uni-popup-dialog>
	</uni-popup>
</template>

<script setup>
	import {
		ref,
		onMounted
	} from "vue"
	import {
		userStore
	} from "../../store/userStrore.js"
	const popup =ref(null)
	const store = userStore()
	const manageList = ref([{
		name: '游玩收藏',
		icon: {
			color: '#0225ff',
			size: '22',
			type: 'star'
		}
	}, {
		name: '我的小组',
		icon: {
			color: '#ff6700',
			size: '22',
			type: 'location-filled'
		}
	}, {
		name: '待开发功能',
		icon: {
			color: '#56c48b',
			size: '22',
			type: 'shop'
		}
	}, {
		name: '退出登录',
		icon: {
			color: '#0081b9',
			size: '22',
			type: 'gear'
		}
	}])

	// Retrieve user info from localStorage on app start
	onMounted(() => {
		const storedUserInfo = localStorage.getItem('userinfo');
		if (storedUserInfo) {
			const userInfo = JSON.parse(storedUserInfo);
			store.addUserInfo(userInfo.username, userInfo.mobilePhoneNumber);
		}
	});

	function toLogin() {
		if (store.isLogin) return; // 已登录则不跳转
		uni.navigateTo({
			url: '/pages/login/login'
		})
	}

	function navto(index) {
		if (!store.isLogin) {
			uni.showToast({
				title: '请先登录',
				icon: 'none'
			});
			return;
		}
	
		switch(index) {
			case 0:
				uni.navigateTo({ url: '/pages/collect/collect' });
				break;
			case 1:
				uni.navigateTo({ url: '/pages/team/team' });
				break;
			case 2:
				title:"暂无该功能"
				break;
			case 3:
				popup.value.open();
				break;
		}
	}

	function confirm(){
		store.clearUserInfo()
		localStorage.removeItem('userinfo'); // Clear stored user info
		uni.showToast({
			title: '退出登录成功',
			icon: 'success',
			duration: 2000
		})
	}
</script>

<style lang="scss">
	.user {
		height: calc(100vh - 97px);
		border: 1px solid #e0e0e0;
		background-color: #f9f9f9;
		border-radius: 10px;
		padding: 20px;
	}

	.userinfo {
		height: 100px;
		background-color: #ffffff;
		border-radius: 10px;
		box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
		padding: 15px;
		display: flex;
		align-items: center;
		position: relative;
		overflow: hidden;
	}

	.userinfo::before {
		content: '';
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-image: url('path/to/your/login-background.jpg');
		background-size: cover;
		background-position: center;
		opacity: 0.1;
		z-index: 0;
	}

	.top {
		display: flex;
		align-items: center;
		z-index: 1;
	}

	.avator {
		width: 60px;
		height: 60px;
		background-color: #e0e0e0;
		border-radius: 50%;
		text-align: center;
		line-height: 60px;
		margin-right: 15px;
	}

	.uname {
		display: flex;
		flex-direction: column;
	}

	.username {
		color: #007AFF;
		font-weight: bold;
		font-size: 18px;
	}

	.mobile {
		color: #666666;
		font-size: 14px;
	}

	.list {
		margin-top: 20px;
		width: 100%;
	}
</style>